package com.zhuiyi.springbootmybatis.controller;

import com.zhuiyi.springbootmybatis.entity.BaseResponse;
import com.zhuiyi.springbootmybatis.entity.Product;
import com.zhuiyi.springbootmybatis.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/product")
@Api(value = "产品", tags = "产品接口")
public class ProductController {


    @Resource
    private ProductService productService;

    /**
     * 获取所有商品
     * @return
     */

    @GetMapping("/getall")
    @ApiOperation(value = "获取所有商品",notes = "获取所有商品")
    public String getAllProducts(ModelMap map) {
        List<Product> products = productService.getProducts();
//        return new BaseResponse(200,"登录成功",products);'
        map.addAttribute("products",products);
        return "products";
    }
    
    /**
     * 添加商品
     * 1.普通方式提交：addProduct( Product product)       2.ajax 方式 提交  参数为json格式 +@RequestBody
     * @return
     */

    @PostMapping("/addproduct")
    @ApiOperation(value = "添加商品",notes = "返回新增商品结果")
    public String addProduct( Product product) {
        boolean data = productService.addProduct(product);
        if(data){
//            新增成功重定向到商品列表页面，更新商品列表
            return "redirect:/product/getall";
        }else{
            return null;
        }

    }

    /**
     * 跳转到添加商品页面
     * @return
     */
    @ApiOperation(value = "跳转到添加商品页面",notes = "跳转到添加商品页面")
    @GetMapping("/addPage")
    public String addPage(){
        return "add";
    }

/**
     * 删除商品 小技巧，删除成功后重定向到商品列表页面，更新商品列表 不用在查询数据库并展示到页面
     * @return
     */


    @GetMapping ("/deleteproduct")
    @ApiOperation(value = "删除商品",notes = "返回删除结果")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, paramType = "query")
    public String deleteProduct( int id) {
        boolean data = productService.delereProductById(id);
//        return new BaseResponse(200,"删除成功",data);
        return "redirect:/product/getall";
    }

    /**
     * 根据id获取商品
     * @return
     */

    @GetMapping("/getproduct")
    @ApiOperation(value = "根据id获取商品",notes = "返回商品")
    @ApiImplicitParam(name = "id", value = "商品id", required = true, paramType = "query")
    public String getProductById(ModelMap map, int id) {
        Product product = productService.getProductById(id);
        map.addAttribute("products",product);
//        return new BaseResponse(200,"查询成功",product);
        return "products";
    }

    /**
     * 更新商品
     * @return
     */

    @GetMapping("/updateproduct")
    @ApiOperation(value = "更新商品",notes = "返回更新结果")
    public String updateProduct( Product product) {
        boolean data = productService.updateProduct(product);
//        return new BaseResponse(200,"更新成功",data);
        return "redirect:/product/getall";
    }
}
